﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL = AccessDAL;
using CommonFunc;

public partial class Manageweb_ManageINews_ManageINews : System.Web.UI.Page
{
    DAL.INewsClassTableAdapters.INewsClassTableAdapter DINC =
        new DAL.INewsClassTableAdapters.INewsClassTableAdapter();

    MODEL.INewsClass MINC = new MODEL.INewsClass();
    MODEL.INewsClass _MINC = new MODEL.INewsClass();
    MODEL.INews MIN = new MODEL.INews();
    MODEL.INews _MIN = new MODEL.INews();

    BLL.INews BIN = new BLL.INews();
    BLL.INewsClass BINC = new BLL.INewsClass();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("../Default.aspx");
        }

        if (!IsPostBack)
        {
            DDL_ClassDataBind();

            SessionInit();

            PageDataInit();
        }
    }


    #region 页面Session [初始化]
    protected void SessionInit()
    {
        try
        {
            if (Request.QueryString["ClassID"] == null)
            {
                Session.Remove("INewsClassCode");
            }
            else
            {
                MINC.INC_ID = Convert.ToInt32(Request.QueryString["ClassID"]);

                _MINC = BINC.GetDataByID(MINC);

                Session["INewsClassCode"] = _MINC.INC_ClassCode;
            }
        }
        catch (Exception ex)
        {
            LB_ManageINews.Text = ex.Message;
        }
    }
    #endregion


    #region 页面数据 [初始化]
    protected void PageDataInit()
    {
        try
        {
            if (Session["INewsClassCode"] != null)
            {
                GV_INewsList.DataSourceID = "ODS_INewsList2";
                GV_INewsList.DataBind();
            }
            else
            {
                GV_INewsList.DataSourceID = "ODS_INewsList";
                GV_INewsList.DataBind();
            }
        }
        catch (Exception ex)
        {
            LB_ManageINews.Text = ex.Message;
        }
    }
    #endregion


    #region DDL_Class数据绑定 [初始化]
    protected void DDL_ClassDataBind()
    {
        try
        {
            DDL_Class.Items.Clear();

            ListItem LI0 = new ListItem("", "");
            DDL_Class.Items.Add(LI0);
            ListItem LI1 = new ListItem("根目录", "0");
            DDL_Class.Items.Add(LI1);

            DataTable DT = DINC.GetAllDataOrderByClassCode();

            foreach (DataRow dr in DT.Rows)
            {
                ListItem li = new ListItem(
                    ClassCode.CreateClassListItemText(dr["INC_Title"].ToString(), dr["INC_Level"].ToString()),
                    dr["INC_ID"].ToString());

                DDL_Class.Items.Add(li);
            }
        }
        catch (Exception ex)
        {
            LB_ManageINews.Text = ex.Message;
        }
    }
    #endregion


    #region 图文图片 [格式化]
    protected string FormatThumb(object _Thumb)
    {
        if (_Thumb.ToString() == "")
        {
            return "~/Manageweb/images/null.jpg";
        }
        else
        {
            return "~/UploadImage/" + _Thumb.ToString();
        }
    }
    #endregion


    #region 修改图文 [GridView] [Command]
    protected void LBtn_Mod_Command(object sender, CommandEventArgs e)
    {
        try
        {
            Response.Redirect("AddINews.aspx?INewsID=" + e.CommandArgument);
        }
        catch (Exception ex)
        {
            LB_ManageINews.Text = ex.Message;
        }
    }
    #endregion


    #region 删除图文 [GridView] [Command] (标记已删除)
    protected void LBtn_Del_Command(object sender, CommandEventArgs e)
    {
        try
        {
            MIN.IN_ID = Convert.ToInt32(e.CommandArgument);

            BIN.MarkIsDel(MIN);

            LB_ManageINews.Text = "删除成功";

            GV_INewsList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageINews.Text = ex.Message;
        }
    }
    #endregion


    #region 搜索按钮 [Click]
    protected void Btn_Search_Click(object sender, EventArgs e)
    {
        try
        {
            GV_INewsList.AllowPaging = false;

            GV_INewsList.DataSourceID = "ODS_Search";

            GV_INewsList.DataBind();
        }
        catch (Exception ex)
        {
            LB_ManageINews.Text = ex.Message;
        }
    }
    #endregion


    #region 添加图文 [Click]
    protected void Btn_AddNews_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("AddINews.aspx?ClassID=" + DDL_Class.SelectedValue);
        }
        catch (Exception ex)
        {
            LB_ManageINews.Text = ex.Message;
        }
    }
    #endregion
}
